package com.insigma.business.qrcode.soldier.util;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.UUID;

@Component
@Slf4j
public class OssUtil {

    private static String proxyHost = "http://img.zhlb.tyjrswj.hangzhou.gov.cn/gongwuyuann";
    private static String ENDPOINT;
    private static String ACCESS_KEY_SECRET;
    private static String ACCESS_KEY_ID;
    private static String BUCKET_NAME;
    private static OSSClient MY_OSS_CLIENT;

    static {
        try {
            ACCESS_KEY_ID = "OGgaOBq8RFWkteBp";
            ACCESS_KEY_SECRET = "cnWCZ0zS9jKNl9PaitREiMtTAqOjJN";
            ENDPOINT = "oss-cn-hangzhou-hzegc-hd01-a.hzegc.cn/gongwuyuann";
            BUCKET_NAME = "gongwuyuann";

            MY_OSS_CLIENT = newClient();
            









            Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
            String ossProxy = properties.getProperty("oss.proxy");
            proxyHost = ossProxy;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (MY_OSS_CLIENT != null) {
                MY_OSS_CLIENT.shutdown();
            }
        }
    }

    private static OSSClient newClient() {
        return new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    }

    public static void main(String[] args) {
        PutObjectResult hello3 = newClient().putObject(BUCKET_NAME, "hello3", new File("E:\\smartSoldier\\code\\smartsoldier-gw-mix\\gateway\\web-shared\\pom.xml"));
        System.out.println(hello3);
    }

    public static String saveFile(File file, String objectKey) {
        MY_OSS_CLIENT = newClient();
        try {
            PutObjectResult p = MY_OSS_CLIENT.putObject(BUCKET_NAME, objectKey, file);
            MY_OSS_CLIENT.shutdown();
            return proxyHost + "/" + objectKey;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
    public static String saveFile(InputStream inputStream, String objectKey) {
        MY_OSS_CLIENT = newClient();
        try {
            PutObjectResult p = MY_OSS_CLIENT.putObject(BUCKET_NAME, objectKey, inputStream);
            MY_OSS_CLIENT.shutdown();
            return objectKey;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    public static void deleteFile(String bucket, String objectKey) {
        MY_OSS_CLIENT = newClient();
        try {
            MY_OSS_CLIENT.deleteObject(bucket, objectKey);
            MY_OSS_CLIENT.shutdown();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static String uploadFileToOss(String url, String idCardNo, String classification, String tmpDir) {
        File imageFile = null;
        try {
            File dirFile = new File(tmpDir);
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }

            String imgName = UUID.randomUUID().toString() + ".jpg";
            imageFile = new File(tmpDir + imgName);
            FileUtils.copyURLToFile(new URL(url), imageFile);
            String objectKey = classification + "/" + idCardNo + "/" + imgName;
            String result = OssUtil.saveFile(imageFile, objectKey);
            return result;
        } catch (Exception e) {
            log.error("uploadFileToOss error : {}", e);
        } finally {
            if (imageFile != null) {
                imageFile.delete();
            }
        }
        return null;
    }
}
